const http = require('http')
const server = http.createServer()
const fs =require('fs')
const path = require('path')

server.listen(3000 ,()=>{
    console.log('服务器启动成功了');
})


server.on('request',(req,res)=>{
    const indexReg = path.join(__dirname,'cloak','index.html')
    if(req.url === '/' || req.url === '/index.html'){
        res.setHeader('content-Type','text/html;charset=utf-8')
        fs.readFile(indexReg,'utf8',(err,data)=>{
            if(err) return console.log(err);
            res.end(data)
        })

    }else if(req.url === '/index.css'){
        const indexReg = path.join(__dirname,'cloak','index.css')
        res.setHeader('content-Type','text/css')
        fs.readFile(indexReg,'utf8',(err,data)=>{
            if(err) return console.log(err);
            res.end(data)
        })
    } else if(req.url === '/index.js'){
        const indexReg = path.join(__dirname,'cloak','index.js')
        res.setHeader('content-Type','application/x-javascript')
        fs.readFile(indexReg,'utf8',(err,data)=>{
            if(err) return console.log(err);
            res.end(data)
        })
    } else{
        res.setHeader('content-Type','text/html;charset=utf-8')
        res.statusCode=404
        res.end('404  !!!!')
    }
})